package com.diyizu.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diyizu.youdianda.common.PageResult;
import com.diyizu.youdianda.common.R;
import com.diyizu.youdianda.dto.YddAdvposDto;
import com.diyizu.youdianda.entity.YddAdvimg;
import com.diyizu.youdianda.entity.YddAdvpos;
import com.diyizu.youdianda.service.YddAdvimgService;
import com.diyizu.youdianda.service.YddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/ydd_advpos")
public class YddAdvposController {

    @Autowired
    private YddAdvposService yddAdvposService;

    @Autowired
    private YddAdvimgService yddAdvimgService;

    /**
     * 广告位列表分页查询,模糊查询
     * @param advposdesc
     * @param advposname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public R page(String advposdesc,String advposname,Integer page,Integer limit){
        log.info("advposdesc = {},advposname = {},page = {},limit = {}",advposdesc,advposname,page,limit);

        //分页构造器
        Page<YddAdvpos> pageInfo = new Page<>(page,limit);

        //条件构造器
        LambdaQueryWrapper<YddAdvpos> wrapper = new LambdaQueryWrapper<>();
        //添加条件
        wrapper.like(StringUtils.isNotEmpty(advposname),YddAdvpos::getAdvposname,advposname);
        wrapper.like(StringUtils.isNotEmpty(advposdesc),YddAdvpos::getAdvposdesc,advposdesc);

        //执行查询条件
        yddAdvposService.page(pageInfo,wrapper);

        //封装条件
        PageResult pageResult = new PageResult();
        pageResult.setTotalPages(pageInfo.getPages());
        pageResult.setPageSize(pageInfo.getSize());
        pageResult.setCurrentPage(pageInfo.getCurrent());
        pageResult.setCount(pageInfo.getTotal());

        //加入广告图数量信息
        List<YddAdvpos> records = pageInfo.getRecords();
        List<YddAdvposDto> list = records.stream().map((item)->{
            //创建YddAvposDto对象
            YddAdvposDto advposDto = new YddAdvposDto();
            BeanUtils.copyProperties(item,advposDto);
            //构建查询条件构造器
            LambdaQueryWrapper<YddAdvimg> lqw = new LambdaQueryWrapper<>();
            //添加查询条件
            lqw.eq(item.getId()!=null,YddAdvimg::getAdvimgpos,item.getId());
            int count = yddAdvimgService.count(lqw);
            advposDto.setAdvposnum(count);

            return advposDto;
        }).collect(Collectors.toList());
        pageResult.setData(list);
        return R.success(pageResult);
    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<YddAdvpos> getById(Integer id){
        log.info("前端传过来的数据:",id);
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        return R.success(yddAdvpos);
    }

    /**
     * 根据id修改广告位
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody YddAdvpos yddAdvpos){
        log.info("前端传过来的数据:",yddAdvpos);
        yddAdvposService.updateById(yddAdvpos);
        return R.success("广告位信息修改成功");
    }

    /**
     * 根据id删除广告位
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> delete(Integer id){
        log.info("前端传过来的数据:",id);
        //根据id判断该数据defaultData是否为默认数据
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        if(yddAdvpos.getDefaultData()!=null&&yddAdvpos.getDefaultData().equals("1")){
           return R.error("不可删除");
        }
        //判断广告图数量是否为0,不为0则不可删除
        LambdaQueryWrapper<YddAdvimg> lqw = new LambdaQueryWrapper<>();
        //添加查询条件
        lqw.eq(yddAdvpos.getId()!=null,YddAdvimg::getAdvimgpos,yddAdvpos.getId());
        int count = yddAdvimgService.count(lqw);
        if(count>0){
            return R.error("广告图数量大于0,不可删除");
        }
        yddAdvposService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 添加广告位
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody YddAdvpos yddAdvpos){
        log.info("前端传过来的数据:",yddAdvpos);
        yddAdvposService.save(yddAdvpos);
        return R.success("广告位添加成功");
    }

    /**
     * 查询全部广告位
     * @return
     */
    @GetMapping("/all")
    public R<List<YddAdvpos>> getAll(){
        List<YddAdvpos> list = yddAdvposService.list();
        return R.success(list);
    }
}
